%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% spine.tex
%% NOVA thesis document template
%%
%% This work is licensed under the
%% The LaTeX project public license (LPPL), version 1.3c
%% To view a copy of this license, visit
%% https://www.latex-project.org/lppl/lppl-1-3c/
%%
%% Authors / Contributors:
%%      - João Lourenço <joao.lourenco@fct.unl.pt>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\typeout{NT FILE spine.tex}%

% Draw the book spine
% usable range: 145 to 425 pages, maximum characters for the title 140 and 22 for the author name
% usable range: 75 to 145 pages, maximum characters for the title 70 and 22 for the author name

\makeatletter

\input{NOVAthesisFiles/fitbox2.sty}

\spine(tab,spec):=?{{@{}c@{}}}

% \def\def@ult#1(#2)=#3{\ifdatadefined{#1}(#2){}{\csname#1\endcsname(#2)={#3}}}
\spine(author):=?{\thedocauthor(name,short)}

\datamatch{\match}{doctitle}({\@LANG@COVER,spine,cover},{\@LANG@COVER,spine})%
\spine(title):=?{\match}

% \typeout{SPINE=[\@LANG@COVER][\thespine(title)]}
\spine(date)=?{\thentdocdate(submission,year)}

% Defaults for spine
\spine(logo):=?{none}
\spine(order):=?{date,title,author,logo}
\spine(order,widthskip):={}
\spine(order,widthskip):={logo,\thespine(order,widthskip)}
\spine(angle):=?{0}
\spine(boxsep):=?{0.5cm}
\spine(image):=?{none}
\spine(bg,color):=?{none}
\spine(frame,color):=?{black!50}
\spine(text,color):=?{black}
\spine(text,align):=?{c}
\spine(margin,top):=?{1mm}
\spine(margin,bottom):=?{1mm}
\spine(margin,left):=?{0.5cm}
\spine(margin,right):=?{0.5cm}
\spine(tikzbg):=?{}

% Defaults for spine boxes
\spine(box,margin):=?{0.5mm}
\spine(box,bg,color):=?{none}
\spine(box,text,color):=?{black}

\spine(box,logo,len):=?{4.1cm}
\spine(box,logo,angle)=?{0}
\spine(box,logo,align):=?{c}
\spine(box,logo,raise):=?{0pt}
\spine(box,logo,scale):=?{1}
\spine(box,logo,margin,left)=?{0mm}
\spine(box,logo,margin,right):=?{0mm}
\spine(box,logo,margin,sep):=?{1mm}

\spine(box,author,len):=?{4.0cm}
\spine(box,author,angle)=?{\thespine(angle)}
\spine(box,author,align)=?{\thespine(text,align)}
\spine(box,author,margin,left):=?{0pt}
\spine(box,author,margin,right):=?{0pt}

\spine(box,title,len):=?{16.7cm}
\spine(box,title,angle)=?{\thespine(angle)}
\spine(box,title,align):=?{\thespine(text,align)}
\spine(box,title,margin,left):=?{0pt}
\spine(box,title,margin,right):=?{0pt}

\spine(box,date,len):=?{2.0cm}
\spine(box,date,angle)=?{\thespine(angle)}
\spine(box,date,align):=?{\thespine(text,align)}
\spine(box,date,margin,left):=?{0pt}
\spine(box,date,margin,right):=?{0pt}

% \typeout{spine(box,logo,len)   = \thespine(box,logo,len)}
% \typeout{spine(box,logo,align) = \thespine(box,logo,align)}
% \typeout{spine(box,author,len) = \thespine(box,author,len)}
% \typeout{spine(box,title,len)  = \thespine(box,title,len)}
% \typeout{spine(box,date,len)   = \thespine(box,date,len)}

\newif\ifisdim
\newcommand{\@setifisdim}[1]{%
  \StrLeft{#1}{1}[\@dimleft]%
  \StrRight{#1}{1}[\@dimright]%
  \IfInteger{\@dimleft}{%
    \IfInteger{\@dimright}{%
      % Is a number
      \isdimfalse%
    }{%
      % May be a dimension
      \isdimtrue%
    }%
  }{%
    % Is a string
    \isdimfalse%
  }%
}
\newcommand{\IfIsDim}[3]{%
  \@setifisdim{#1}%
  \ifisdim#2\else#3\fi%
}

\ifoptioncontains{/novathesis/debug}{spine}{%
  \def\@spineboxdrawopacity{1}%
  \newcommand{\DEBUG}{\NTDEBUG}%
}{%
  \def\@spineboxdrawopacity{0}%
  \newcommand{\DEBUG}[2][]{}%
}%

\newdimen\@spinelen  % total spine length
\newcommand{\@checkspinelen}[1][\paperheight]{%
  \@tempdima=\thespine(margin,left)\relax%
  \@tempdimb=\thespine(margin,right)\relax%
  \DEBUG[\@tempdima]{SPINELEN MARGIN [left]}%
  \DEBUG[\@tempdimb]{SPINELEN MARGIN [right]}%
  \@tempdima=\dimexpr\@tempdima+\@tempdimb\relax%
  \DEBUG[\@tempdima]{SPINELEN ACCUMULATED}%
  \StrCount{\thespine(order)}{,}[\@nBoxSep]%
  \@for\myi:=\expanded{\thespine(order)}\do{%
    \@tempdimb=\thespine(box,\myi,len)\relax%
    \DEBUG[\@tempdimb]{SPINELEN BOXLEN [\myi]}%
    \@tempdima=\dimexpr\@tempdima+\@tempdimb\relax%
    \DEBUG[\@tempdima]{SPINELEN ACCUMULATED}%
  }
  \DEBUG[#1]{SPINELEN MAX}%
  \@spinelen=\@tempdima%
}


\newcommand{\@reverelist}[2]{%
%   % #1 = list to be reversed
  \def\@rvstmp{}%
  \@for\myi:={#1}\do{%
    \epreto\@rvstmp{,\myi}%
  }%
  \edef\@rvstmp{\expandafter\@gobble\@rvstmp}%
  \edef#2{\@rvstmp}%
}


\providecommand*{\DivideLengths}[2]{%
  \strip@pt\dimexpr\number\numexpr\number\dimexpr#1\relax*65536/\number\dimexpr#2\relax\relax sp\relax
}


\newcommand{\@setlength}[2]{%
  \defifundef{#1}{\newlength}%
  \@tempdima=\dimexpr#2\relax%
  \setlength{#1}{\the\@tempdima}%
}

\newcommand{\ifnotnone}[2]{%
  \IfStrEq{#1}{none}{}{#2}%
}



\newcommand{\@ntprintspine}{%x§
% If angle is 0 revere the order of the logos
\IfStrEqCase{\thespine(angle)}{%
    {0}{\@reverelist{\expanded{\thespine(logo)}}\@logoorder%
        \spine(logo):={\@logoorder}}%
  {180}{\spine(margin,left2):={\thespine(margin,left)}%
        \spine(margin,left):={\thespine(margin,right)}%
        \spine(margin,right):={\thespine(margin,left2)}%
        % \spine(box,logo,angle):={\number\numexpr\thespine(box,logo,angle)+180\relax}%
        \@reverelist{\expanded{\thespine(order)}}\spineorder%
        \spine(order):={\spineorder}}%
}[\CalssError{novathesis}{Invalid spine angle: \thespine(angle)! Should be wither “0” or “180”.}{}]


\@checkspinelen

% Increase box spacing if necessary
% Reduce default boxes length and keep given/forces boxes length 
\@tempdima=\dimexpr\paperheight-\@spinelen\relax%                             = remaining space to fill spine
\ifdim\@tempdima>0pt\relax%
  % spine FITS into paper height
  \@tempdima=\dimexpr\@tempdima/\@nBoxSep\relax%                              = new box sep size
  % \DEBUG[\@tempdima]{NEW BOXS}
  \spine(boxsep):={\the\@tempdima}%                                           = set new boxsep
\else\ifdim\@tempdima<0pt\relax%
  % spine DOES NOT FIT into paper height
    \ClassError{novathesis}{Spine is \the\@tempdima\space too long: \the\@spinelen\space instead of \the\paperheight}
    {Get some insight with “fgrep SPINELEN *.log”}{}
\fi\fi%
  % % SPINE boxes are to large for the spine size, decrease spacing in the not-explicitly defined boxes
  % \@tempdima=\dimexpr\paperheight-\@spinefixedlen\relax
  % % \showthe\@tempdima
  % \@tempdimb=\dimexpr\@spinelen-\@spinefixedlen\relax
  % % \showthe\@tempdimb
  % % \StrGobbleRight{\the\@tempdimb}{2}[\@SPVAR]
  % \DEBUG{DL 1=[\the\@tempdima] 2=[\the\@tempdimb]}\AAAA
  % \edef\@spinevarratio{\DivideLengths{\@tempdima}{\@tempdimb}}
  % % \show\@spinevarratio
  % \@for\myi:=\expanded{\thespine(order)}\do{%
  %   \ifdatadefined{spine}(box,\myi,len){%
  %     % Fixed box length, do not touch
  %   }{%
  %     % Variable box length, apply ratio
  %   \@tempdima=\dimexpr\thespine(box,\myi,len)\relax%
  %   % \DEBUG{OLD SPINEBOXLEN[\myi]=\the\@tempdima}
  %   \@tempdima=\dimexpr \@spinevarratio\@tempdima\relax
  %   % \DEBUG{NEW SPINEBOXLEN[\myi]=\the\@tempdima}
  %   \spine(box,\myi,len):={\the\@tempdima}
  %   }%
  % }%

% \displayspinealyout
\@checkspinelen
% \DEBUG[\paperheight]{PAPER HEIG}
% \DEBUG[\@spinelen]{SPINELEN[FINA}
% \PAUSETHIS

% Calculate box[lef], box[len], box[right] if undefined
\@tempdima=\dimexpr\thespine(margin,left)-\thespine(boxsep)\relax%
\@for\myi:=\expanded{\thespine(order)}\do{
  \ifdatadefined{spine}(box,\myi,left){%
    \@tempdima=\thespine(box,\myi,left)%
  }{%
    \@tempdima=\dimexpr\@tempdima+\thespine(boxsep)\relax%
  }%
  \spine(box,\myi,left):={\the\@tempdima}%
  \@tempdima=\dimexpr\@tempdima+\thespine(box,\myi,len)\relax%
  \spine(box,\myi,right):={\the\@tempdima}
  \DEBUG[\thespine(box,\myi,left)]{BOX[\myi,left]}
  \DEBUG[\thespine(box,\myi,len)]{BOX[\myi,len]}
  \DEBUG[\thespine(box,\myi,right)]{BOX[\myi,right]}
}%

\@for\myi:=\expanded{\thespine(order)}\do{%
  % \DEBUG{TEXT[\myi]}
  \datamatchtf{\@boxml}{spine}({box,\myi,margin,left},{box,margin,left}){}{\def\@boxml{0pt}}%
  \datamatchtf{\@boxmr}{spine}({box,\myi,margin,right},{box,margin,right}){}{\def\@boxmr{0pt}}%
  % \DEBUG{TEXT[\myi] [\@boxml] [\@boxmr]}
  \@tempdima=\dimexpr\thespine(box,\myi,len)\relax%
  \spine(box,\myi,len):={\the\@tempdima}%
  \@tempdima=\dimexpr\@tempdima-\@boxml-\@boxmr\relax%
  \spine(box,\myi,len2):={\the\@tempdima}%
  % \DEBUG{TEXT[\myi,len]=\thespine(box,\myi,len)}
  % \DEBUG{TEXT[\myi,len2]=\thespine(box,\myi,len2)}
}
\ifoptionequal{/novathesis/spine/layout}{none}{}{%
  \parindent=0pt%
  \sffamily%
  \linespread{0.85}%
  \normalsize%
  \ifdim\option{/novathesis/spine/width}=0pt\relax%
    \@setlength{\@spinewidth}{0.05mm * (\thelastsheet+1)}%
    % \DEBUG{NOVATHESIS spine width for \thelastsheet\space pages is \the\@spinewidth!}%
    \ifdim\@spinewidth < 6mm\relax% Force book spine to be at least 6mm
      % \DEBUG{NOVATHESIS spine width was \the\@spinewidth, forcing to 6mm!}%
      \@setlength{\@spinewidth}{6mm}%
    \fi%
  \else%
    \@setlength{\@spinewidth}{\option{/novathesis/spine/width}}%
    % \DEBUG{NOVATHESIS spine width user forced by user to \the\@spinewidth!}%
  \fi%
  % \DEBUG{NOVATHESIS \string\@spinewidth=\the\@spinewidth}%
  \@setlength{\@thespinewidth}
             {\dimexpr\@spinewidth-\thespine(margin,top)-\thespine(margin,bottom)\relax}%
  \def\@fbratio{9999}%
  \@for\myi:=\expanded{\thespine(order)}\do{%
    \IfStrEq{\myi}{title}{%
      \fitboxratio{@\myi RT}{\thespine(box,\myi,len2)}{\@thespinewidth}{\thespine(\myi)}%
      \fitboxratio{@\myi 2RT}{\thespine(box,\myi,len2)}{\@thespinewidth}{\def\\{ }\thespine(\myi)}%
      \pgfmathparse{\csuse{@\myi RT}}%
      \edef\@tone{\pgfmathresult}%
      \pgfmathparse{\csuse{@\myi 2RT}}%
      \edef\@ttwo{\pgfmathresult}%
      % \DEBUG{T1=\@tone\space T2=\@ttwo}\AAAAA%
      \ifdim\@tone pt < \@ttwo pt\relax%
        % \BBBBBBB%
        \spine(\myi):={\def\\{ }\thespine(\myi)}%
      \fi%
    }{}%\ASDSDDSAS
    \IfSubStr{,\thespine(order,widthskip),}{,\myi,}{}{%
      \fitboxratio{@\myi RT}{\thespine(box,\myi,len2)}{\@thespinewidth}{\thespine(\myi)}%
      % \DEBUG{NOVATHESIS RATIO[\myi]=\csuse{@\myi RT}}%
      \pgfmathparse{min(\@fbratio,\csuse{@\myi RT})}%
      % \DEBUG{NOVATHESIS RATIO MIN(\@fbratio,\csuse{@\myi RT})=\pgfmathresult}%
      \edef\@fbratio{\pgfmathresult}%
    }%
  }%
  % \DEBUG{NOVATHESIS RATIO=\@fbratio}
  \currentpdfbookmark{\thebkmstring(spine)}{thespine}%
  \ifoptionequal{/novathesis/spine/layout}{trim}{%
    \stockwidth=\stockheight%
    \stockheight=\@spinewidth%
  }{\ifoptionequal{/novathesis/spine/layout}{full}{%
    \let\oldstockwidth=\stockwidth
    \stockwidth=\stockheight%
    \stockheight=21cm%
  }{%
    \ClassError{novathesis}{Unknown page dimensions for “spine/layout=\option{/novathesis/spine/layout}”}
  }}%
  \paperwidth=\stockwidth%
  \paperheight=\stockheight%
  \setstocksize{\stockheight}{\stockwidth}%
  \settrimmedsize{\stockheight}{\stockwidth}{*}%
  % \paperheight=\@spinewidth%
  \setlrmarginsandblock{0pt}{0pt}{*}%
  \setulmarginsandblock{0pt}{*}{1}%
  \setheaderspaces{0pt}{*}{*}%
  \footskip = 0pt
  \checkandfixthelayout[fixed]%
  \ifluatex%
  \pagewidth=\paperwidth%
  \pageheight=\paperheight% \pageheight=3in
  \else%
  \pdfpagewidth=\paperwidth%
  \pdfpageheight=\paperheight% \pageheight=3in
  \fi%
  \thispagestyle{empty}%
  \NTRunHook{spine/pre}%
  % \PAUSETHIS
 \ifoptionequal{/novathesis/spine/layout}{trim}
               {\@tempdima=0pt}
               {\@tempdima=\dimexpr(\stockheight-\@spinewidth)/2\relax}%
  \begin{tikzpicture}[remember picture, overlay,yscale=-1,yshift=-10.75pt+\@tempdima]%
    % print spine bg color IF DEFINED
    \datamatchtf{\arg}{spine}(%
                      {\@DOCTYPE,bg,color},%
                      {\@DOCCLASS,bg,color},%
                      {bg,color}%
    ){\ifnotnone{\arg}{\pagecolor{\arg}}}{%
      \ClassError{novathesis}{Undefined spine “bg,color”}{}%
    }%
    % print frame IF DEFINED
    \ifoptionequal{/novathesis/spine/layout}{full}{%
      \datamatchtf{\arg}{spine}(%
                        {\@DOCTYPE,frame,color},%
                        {\@DOCCLASS,frame,color},%
                        {frame,color}%
      ){\ifnotnone{\arg}{\draw [color=\arg] (0,0) rectangle (\paperwidth-0.4pt,\@spinewidth-0.4pt);}}{%
        \ClassError{novathesis}{Undefined spine “frame,color”}{}%
      }%
    }{}%
    % print bg image IF DEFINED
    \datamatchtf{\arg}{thesiscover}({\@DOCTYPE,spine,image},{\@DOCCLASS,spine,image},{spine,image}){%
      \ifnotnone{\arg}{%
        \node[inner sep=0] at (current page.center)
                {\includegraphics[width=\paperheight,height=\paperwidth,
                                  angle=\number\numexpr\thespine(angle)-90\relax]{\arg}};
      }}{%
          \datamatchtf{\arg}{spine}(%
                        {\@DOCTYPE,image},%
                        {\@DOCCLASS,image},%
                        {image},%
      ){%
          \ifnotnone{\arg}{%
            \node[inner sep=0] at (current page.center)
                    {\includegraphics[width=\paperheight,height=\paperwidth,
                                      angle=\number\numexpr\thespine(angle)-90\relax]{\arg}};
      }}{%
        \ClassError{novathesis}{Undefined spine “image”}{}%
      }%
    }%
    \@for\myi:=\expanded{\thespine(order)}\do{%
      \datamatch{\@boxcolor}{spine}({box,\myi,bg,color},{box,bg,color})%
      \node[rectangle,
            inner sep = 0pt,
            anchor = north west,
            draw, color = red, draw opacity=\@spineboxdrawopacity,
            fill = {\@boxcolor},
            minimum width = \expanded{\thespine(box,\myi,len)},
            minimum height = \@thespinewidth,
      ] (r\myi) at (\thespine(box,\myi,left),\expanded{\thespine(margin,top)}) {};
      \datamatchtf{\@align}{spine}({box,\myi,align},{\myi,align},{text,align}){}{%
        \ClassError{novathesis}{Invalid spine text alignment for “\myi”}{}%
      }%
      \IfStrEqCase{\@align}{%
        {l}{\node [inner sep=0, anchor=west] at (r\myi.west)
              {\printspinebox{\myi}{\@fbratio}};}%
        {c}{\node [inner sep=0, anchor=center] at (r\myi)
              {\printspinebox{\myi}{\@fbratio}};}%
        {r}{\node [inner sep=0, anchor=east] at (r\myi.east)
              {\printspinebox{\myi}{\@fbratio}};}%
      }[{\node [inner sep=0, anchor=center] at (r\myi)
              {\printspinebox{\myi}{\@fbratio}};}]%
    }%
  \end{tikzpicture}%
  \ifoptioncontains{/novathesis/DEBUG}{cover}{%
    \NTRunHook{cover/spine/grid/pre}%
    \DEBUGgrid%
    \NTRunHook{cover/spine/grid/post}%
  }{}%
}%
}

\newcommand{\printspinebox}[2]{%
  % 1=spine box name
  % 2=ratio
  \datamatchtf{\@item}{spine}(%
                    {#1,\@DOCCLASS},%
                    {#1,\@DOCTYPE},%
                    {#1}%
  ){}{\ClassError{novathesis}{Undefined spine element: #1}{}}%
  \datamatchtf{\@angle}{spine}(%
                    {box,#1,\@DOCTYPE,angle},%
                    {box,#1,\@DOCCLASS,angle},%
                    {box,#1,angle},%
                    {#1,\@DOCTYPE,angle},%
                    {#1,\@DOCCLASS,angle},%
                    {#1,angle}%
  ){}{\def\@angle{\thespine(angle)}}%
  \datamatchtf{\@scale}{spine}(%
                    {box,#1,\@DOCTYPE,scale},%
                    {box,#1,\@DOCCLASS,scale},%
                    {box,#1,scale},%
                    {#1,\@DOCTYPE,scale},%
                    {#1,\@DOCCLASS,scale},%
                    {#1,scale}%
  ){}{\def\@scale{1}}%
  \datamatchtf{\@raise}{spine}(%
                    {box,#1,\@DOCTYPE,raise},%
                    {box,#1,\@DOCCLASS,raise},%
                    {box,#1,raise},%
                    {#1,\@DOCTYPE,raise},%
                    {#1,\@DOCCLASS,raise},%
                    {#1,raise}%
  ){}{\def\@raise{0pt}}%
  % \DEBUG{L=[\thespine(logo)] IT(#1)=[\@item]}%
  % \DEBUG{PRINT BOX #1  ITEM=\@item}%
  % \DEBUG{PRINT BOX #1 ANGLE=\@angle}%
  % \DEBUG{PRINT BOX #1 SCALE=\@scale}%
  % \DEBUG{PRINT BOX #1 RAISE=\@raise}%
  % \AAAA%
  \IfStrEq{#1}{logo}{%
    % IMAGE
    \IfStrEq{\@item}{none}{}{%
      \edef\@lspace{\thespine(box,logo,margin,sep)}%
      \edef\@lwidth{\thespine(box,logo,len2)}%
      \edef\@lheight{\@thespinewidth}%
      % \DEBUG{W=\@lwidth\space H=\the\@lheight\space S=\@lspace}\FILE
      \setbox1=\hbox{\hspace*{-\@lspace}}%
      \@for\myj:={\@item}\do{%
        \StrCut{\myj}{/}\@@lspace\@lfile%
        % \DEBUG{LS=\@@lspace\space F=\@lfile}\FILE
        \IfIsDim{\@@lspace}{}{\edef\@lfile{\@@lspace/\@lfile}\edef\@@lspace{0pt}}%
        \StrCut{\@lfile}{/}\@lfile\@langle%
        % \DEBUG{F=\@lfile\space A=\@langle}\FILE
        \StrCut{\@langle}{/}\@langle\@lscale%
        % \DEBUG{F=\@lfile\space A=\@langle\space S=\@lscale}\FILE
        \IfStrEq{\@langle}{}{\edef\@langle{\@angle}}{}%
        \IfStrEq{\@lscale}{}{\edef\@lscale{\@scale}}{}%
        % \DEBUG{F=\@lfile\space A=\@langle\space S=\@lscale}\FILE
        % \setbox0=\hbox to \thespine(box,logo,len){\includegraphics[height=\@thespinewidth,angle=\number\numexpr\@langle-90\relax,scale=\@lscale,origin=c]{\@lfile}}%
        \setbox0=\hbox{\includegraphics[height=\@thespinewidth,angle=\number\numexpr\@langle-90\relax,scale=\@lscale,origin=c]{\@lfile}}%
        \setbox1=\hbox{\minipage{\wd1}\box1\endminipage\hspace*{\@lspace}\minipage{\wd1}\box0\endminipage\hspace{\@@lspace}}%
      }
      \ifdim\dimexpr\wd1\relax>\@lwidth\relax%
        \setbox1=\hbox{\resizebox*{\@lwidth}{!}{\box1}}%
      \fi%
      \ifdim\dimexpr\ht1+\dp1\relax>\@lheight\relax%
        \setbox1=\hbox{\rotatebox{-90}{\box1}}%
        \setbox1=\hbox{\resizebox*{\@lheight}{!}{\box1}}%
        \setbox1=\hbox{\rotatebox{90}{\box1}}%
      \fi%
      \box1%
    }%
  }{%
    % \DEBUG{IT=[#1] V=[\thespine(#1)]}\AAAAA%
    % TEXT
    \rotatebox[origin=c]{\@angle}{%
    \scalebox{\@fbratio}{%
      \datamatchtf{\arg}{spine}({box,#1,text,gz},{box,text,color},{text,color}){\color{\arg}}{}%
      \ifdatadefined{spine}(#1,\@UNIV/\@SCHL){%
        \tabular{@{}c@{}}\thespine(#1,\@UNIV/\@SCHL)\endtabular%
      }{%
        \edef\@IT{#1}
        \edef\@TABSPEC{\thespine(tab,spec)}%
        \thespine(#1,font)\expandafter\tabular\@TABSPEC%
        \ifdatadefined{spine}(#1,1)
            {\@for\myi:={1,2,3,4,5,6,7,8,9}\do{%
                \ifdatadefined{spine}(\@IT,\myi)
                    {\thespine(\@IT,\myi)}
                    {}%
                }}
            {\thespine(#1)}%
        \endtabular%
      }%
    }%
    }%
  }%
  % \PAUSETHIS
}

